﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using libtcodWrapper;
using Console = libtcodWrapper.Console;

namespace HDHack
{
    class EquipItemInterface
    {

        Console displayConsole;
        public bool IsActive = false;
        Dungeon theDungeon;
        Player p;
        List<Item> EquipableItems;

        public EquipItemInterface(Player ip, Console dCon, Dungeon d)
        {
            p = ip;
            displayConsole = dCon;
            theDungeon = d;
        }

        private List<Item> GetEquipableItems(List<Item> il)
        {
            List<Item> eList = new List<Item>();
            foreach(Item i in il)
            {
                if (i.Equipable)
                {
                    eList.Add(i);
                }
            }
            return eList;
        }

        int ConvertItemID(int charVal)
        {
            int retVal = charVal - 97;
            return retVal;
        }

        public void ProcessChoice(KeyPress k)
        {
            int choiceIndex = ConvertItemID(k.Character);
            if (Convert.ToInt16(k.Character) > 96 && choiceIndex < EquipableItems.Count)
            {
                EquipableItems[choiceIndex].OnMap = false;                
                p.EquipItem(EquipableItems[choiceIndex]);
                EquipableItems[choiceIndex].IsEquiped = true;
                EquipableItems.Clear();
                displayConsole.Clear();
                IsActive = false;
                theDungeon.ComplexCommand = false;
            }
            else if (k.KeyCode == KeyCode.TCODK_ESCAPE)
            {
                displayConsole.Clear();
                IsActive = false;
                theDungeon.ComplexCommand = false;
            }
        }

        public void DisplayEquipedItems()
        {


            displayConsole.Clear();
            int lastline = 1;

            lastline = displayConsole.PrintLineRect("Equiped Items:", 1, 1, displayConsole.GetConsoleWidth(), displayConsole.GetConsoleHeight(), LineAlignment.Left);
            displayConsole.PrintLine("", 1, lastline, LineAlignment.Left);
            lastline++;
            lastline++;

            foreach (BodyPart bp in p.BodyParts)
            {
                string itemName = "empty";
                if (bp.EquipedItem != null)
                    itemName = bp.EquipedItem.name;
                lastline = lastline + displayConsole.PrintLineRect(bp.name + "-" + itemName, 1, lastline, displayConsole.GetConsoleWidth(), displayConsole.GetConsoleHeight(), LineAlignment.Left);
                displayConsole.PrintLine("", 1, lastline, LineAlignment.Left);
                lastline++;

            }
        }

        public void DisplayEquipableInventory()
        {            
            displayConsole.Clear();
            int lastline = 1;
            int itemASCIIIndex = 97;
            EquipableItems = GetEquipableItems(p.Inventory);

                        
            if (EquipableItems.Count > 0)
            {
                lastline = displayConsole.PrintLineRect("Equip which item?", 1, 1, displayConsole.GetConsoleWidth(), displayConsole.GetConsoleHeight(), LineAlignment.Left);
                lastline++;
                displayConsole.PrintLine("", 1, lastline, LineAlignment.Left);
                lastline++;


                foreach (Item i in EquipableItems)
                {
                    displayConsole.PrintLine("(" + Convert.ToChar(itemASCIIIndex) + ") " + i.name, 1, lastline, LineAlignment.Left);
                    itemASCIIIndex++;
                    lastline++;

                }                
                IsActive = true;

            }
            else
            {
                displayConsole.PrintLine("Nothing to equip!", 1, lastline, LineAlignment.Left);
                theDungeon.ComplexCommand = false;
                IsActive = false;

            }
        }


    }
}
